package com.example.service.appointment;

import cn.hutool.core.date.DateUtil;

import com.example.common.enums.RoleEnum;
import com.example.mapper.other.ChatInfoMapper;
import com.example.mapper.other.ChatRoleMapper;
import com.example.model.entity.other.Account;
import com.example.model.entity.other.ChatGroup;
import com.example.model.entity.other.ChatInfo;
import com.example.model.entity.other.ChatRole;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 聊天信息业务处理
 **/
@Service
public class ChatInfoService {

    @Resource
    private ChatInfoMapper chatInfoMapper;
    @Resource
    private ChatRoleMapper chatRoleMapper;

    /**
     * 新增
     */
    public void add(ChatInfo chatInfo) {
        Account currentUser = TokenUtils.getCurrentUser();
        ChatRole chatRole=null;
        ChatRole userRole=null;
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByDoctorId(currentUser.getId());
            userRole = chatRoleMapper.selectByUserId(chatInfo.getChatUserId());
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByUserId(currentUser.getId());
            userRole = chatRoleMapper.selectByDoctorId(chatInfo.getChatUserId());
        }
        ChatInfo info=new ChatInfo();
        info.setChatUserId(userRole.getId());
        info.setUserId(chatRole.getId());
        info.setTime(DateUtil.now());
        info.setText(chatInfo.getText());
        chatInfoMapper.insert(info);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        chatInfoMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            chatInfoMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(ChatInfo chatInfo) {
        chatInfoMapper.updateById(chatInfo);
    }

    /**
     * 根据ID查询
     */
    public ChatInfo selectById(Integer id) {
        return chatInfoMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ChatInfo> selectAll(ChatInfo chatInfo) {
        return chatInfoMapper.selectAll(chatInfo);
    }

    /**
     * 分页查询
     */
    public PageInfo<ChatInfo> selectPage(ChatInfo chatInfo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ChatInfo> list = chatInfoMapper.selectAll(chatInfo);
        return PageInfo.of(list);
    }

    public Integer selectUnReadChatNum(Integer userId, Integer chatUserId) {
        return chatInfoMapper.selectUnReadChatNum(userId, chatUserId);
    }


    public List<ChatInfo> selectUserChat(Integer chatUserId,String role) {
        Account currentUser = TokenUtils.getCurrentUser();
        ChatRole chatRole=null;
        ChatRole userRole=null;
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByDoctorId(currentUser.getId());
            if(RoleEnum.USER.name().equals(role)){
                userRole = chatRoleMapper.selectByUserId(chatUserId);
            }else if(RoleEnum.BUSINESS.name().equals(role)){
                userRole = chatRoleMapper.selectByBusinessId(chatUserId);
            }
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByUserId(currentUser.getId());
            if(RoleEnum.DOCTOR.name().equals(role)){
                userRole = chatRoleMapper.selectByDoctorId(chatUserId);
            }else if(RoleEnum.BUSINESS.name().equals(role)){
                userRole = chatRoleMapper.selectByBusinessId(chatUserId);
            }
        }
        List<ChatInfo> chatInfos = chatInfoMapper.selectUserChat(chatRole.getId(), userRole.getId());
        for (ChatInfo chatInfo : chatInfos) {
            if (RoleEnum.DOCTOR.name().equals(currentUser.getRole())) {
                chatInfo.setRoleId(chatRole.getId());
            } else if (RoleEnum.USER.name().equals(currentUser.getRole())) {
                chatInfo.setRoleId(chatRole.getId());
            }
        }
        return chatInfos;
    }

    public void updateRead(Integer chatUserId,String role) {
        Account currentUser = TokenUtils.getCurrentUser();
        ChatRole chatRole=null;
        ChatRole userRole=null;
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByDoctorId(currentUser.getId());
            if(RoleEnum.USER.name().equals(role)){
                userRole = chatRoleMapper.selectByUserId(chatUserId);
            }else if(RoleEnum.BUSINESS.name().equals(role)){
                userRole = chatRoleMapper.selectByBusinessId(chatUserId);
            }
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByUserId(currentUser.getId());
            if(RoleEnum.DOCTOR.name().equals(role)){
                userRole = chatRoleMapper.selectByDoctorId(chatUserId);
            }else if(RoleEnum.BUSINESS.name().equals(role)){
                userRole = chatRoleMapper.selectByBusinessId(chatUserId);
            }
        }
        chatInfoMapper.updateRead(chatRole.getId(), userRole.getId());
    }

}
